package org.yn.mkt.controller;


import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yn.base.util.EasyExcelUtils;
import org.yn.mkt.domain.Clue;
import org.yn.mkt.service.IClueService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RequestMapping("/cluedown")
@Controller
public class ClueDownLoadController {
    @Autowired
    public IClueService clueService;

    @PostMapping("/dowmloadExcel")
    public void dowmloadExcel(HttpServletResponse response) throws IOException {
        System.out.println("-------");
        List<Clue> clueList = clueService.selectAll();
        clueList.forEach(System.out::println);
// 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Clue.class).sheet("模板").doWrite(clueList);




    }
}
